﻿using CatUtils.Utils.Currency.IoUtils;
using ImageMagick;

using System;
using System.IO;

namespace RabbitDownload.Helper.Currency
{
    internal class MagickImageHelper
    {
        /// <summary>
        /// 重新调整大小
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="SizeKB"></param>
        /// <param name="Percentage"></param>
        public static void CompressimgToSize(string FilePath, int SizeKB, int Percentage = 90, string Tip = "缩放压缩")
        {
            double FileSize = FileHelper.GetFileSizeForKB(FilePath);
            string TempFilePath = "";
            Console.WriteLine("进入文件大小{0},预期大小{1}", FileSize, SizeKB);
            if (FileSize > SizeKB)
            {
                TempFilePath = ImgResizeForPercentage(FilePath, Percentage, Tip);
                if (TempFilePath == "")
                    return;
                FileSize = FileHelper.GetFileSizeForKB(TempFilePath);

                if (FileSize > SizeKB)
                {
                    if (Percentage > 20)
                        Percentage = Percentage - 10;
                    else
                        Percentage = Percentage - 2;
                    Console.WriteLine("进入文件大小{0},预期大小{1}", FileSize, SizeKB);
                    if (Percentage > 1)
                        CompressimgToSize(FilePath, SizeKB, Percentage);
                    else
                    {
                        Console.WriteLine("图片过大，无法达到要求");
                    }
                }
            }
            else
            {
                return;
            }
        }

        /// <summary>
        /// 自动选择比例压缩，保持图片尺寸不变
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Tip"></param>
        public static void AutoCompressImg(String FilePath, string Tip)
        {
            double FileSize = FileHelper.GetFileSizeForKB(FilePath);

            if (FileSize < 300)
            {
                CompressImg(FilePath);
            }
            else if (FileSize < 700)
            {
                CompressImgForQuality(FilePath, 90, Tip);
            }
            else if (FileSize < 1000)
            {
                CompressImgForQuality(FilePath, 80, Tip);
            }
            else if (FileSize < 2000)
            {
                CompressImgForQuality(FilePath, 70, Tip);
            }
            else if (FileSize < 3000)
            {
                CompressImgForQuality(FilePath, 60, Tip);
            }
            else if (FileSize < 4000)
            {
                CompressImgForQuality(FilePath, 50, Tip);
            }
            else if (FileSize < 5000)
            {
                CompressImgForQuality(FilePath, 40, Tip);
            }
            else
            {
                CompressImgForQuality(FilePath, 30, Tip);
            }
        }

        /// <summary>
        /// 根据指定的宽高调整图片大小
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="Width"></param>
        /// <param name="Height"></param>
        /// <returns></returns>
        public static string ImgResize(String FilePath, int Width, int Height)
        {
            string ChildPath = "";

            using (MagickImage image = new MagickImage(FilePath))
            {
                MagickGeometry size = new MagickGeometry(Width, Width);
                // This will resize the image to a fixed size without maintaining the aspect ratio.
                // Normally an image will be resized to fit inside the specified size.

                image.Resize(size);

                // Save the result
                ChildPath = MagickImageWrite(FilePath, image, string.Format("{0}x{1}", Width, Height));
            }

            return ChildPath;
        }

        /// <summary>
        /// 写入文件
        /// </summary>
        /// <param name="FilePath"></param>
        /// <param name="image"></param>
        /// <param name="Tip"></param>
        private static string MagickImageWrite(string FilePath, MagickImage image, string Tip = "")
        {
            String ChildPath = "";

            if (Tip == "")
                ChildPath = FloderNameHelper.GetFloderPath(FilePath);
            else
                ChildPath = FloderNameHelper.GetFloderPath(FilePath) + Tip + "\\";
            if (FloderHelper.FloderExits(ChildPath, true))
            {
                ChildPath = ChildPath + Path.GetFileName(FilePath);
                image.Write(ChildPath);
            }
            return ChildPath;
        }

        /// <summary>
        /// 无损压缩并且覆盖
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static bool CompressImg(String FilePath)
        {
            FileInfo snakewareLogo = new FileInfo(FilePath);

            ImageOptimizer optimizer = new ImageOptimizer();

            optimizer.LosslessCompress(snakewareLogo);

            snakewareLogo.Refresh();

            return true;
        }

        /// <summary>
        /// 缩放压缩
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string ImgResizeForPercentage(String FilePath, int PercentageCount = 90, string Tip = "缩放压缩")
        {
            string ChildPath = "";
            using (MagickImage image = new MagickImage(FilePath))
            {
                image.Scale(new Percentage(PercentageCount));

                ChildPath = MagickImageWrite(FilePath, image, Tip);
            }
            return ChildPath;
        }

        /// <summary>
        /// 质量压缩模式 不缩放图片
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static string CompressImgForQuality(String FilePath, int PercentageCount, string Tip = "不缩放压缩")
        {
            string ChildPath = "";
            using (MagickImage image = new MagickImage(FilePath))
            {
                image.Quality = PercentageCount;

                ChildPath = MagickImageWrite(FilePath, image, Tip);
            }
            return ChildPath;
        }
    }
}